const $table = document.querySelector('table');
function showTable(data) {
    $table.innerHTML = '';
    let html = (data.map(objToString)
        .map(row => `<tr>${row}</tr>`)
        .join(''));
    const header= tableHeader(data);
    console.log(header);
    $table.innerHTML = `<tr>${header}</tr>${html}`;
}
function tableHeader(data) {
    return Object.keys(data[0]).map(o => `<td>${o}</td>`).join('');
}

function objToString(obj) {
    return Object.values(obj).map(o => `<td>${o}</td>`).join('');
}

document.onclick = e => {
    const table = e.target.value;
    const url = `http://192.168.189.131:3000/${table}`;
    // console.log(url);
    // console.log(objToString({name:'lisi', age:20}));
    if(!table) return;
    fetch(url)
        .then(res => res.json())
        .then(rows => {
            // console.table(rows);
            showTable(rows);
        })
}